package com.lanou.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lanou.bean.Cost;
import com.lanou.mapper.CostMapper;
import com.lanou.service.CostService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by dllo on 18/3/13.
 */
@Service
public class CostServiceImpl implements CostService {
    @Resource
    private CostMapper costMapper;

    @Override
    public List<Cost> allcost() {
        return costMapper.selectcost();
    }

    @Override
    public PageInfo<Cost> queryCostByPage(Integer pageNo, Integer pageSize) {
        pageNo = pageNo == null ? 1 : pageNo;
        pageSize = pageSize == null ? 2 : pageSize;
        PageHelper.startPage(pageNo, pageSize);
        List<Cost> costList = allcost();
        PageInfo<Cost> CostPageInfo = new PageInfo<Cost>(costList);
        return CostPageInfo;
    }

    @Override
    public void addAll(String name, Integer baseDuration, Integer baseCost, Integer unitCost, String descr, String costType) {
        int id = (int) (Math.random() * 9 + 1);
        String costid = String.valueOf(id);
        int costId = Integer.parseInt(costid);
        Date creatime = new Date(System.currentTimeMillis());
        Date startime = new Date(System.currentTimeMillis());
        Cost cost = new Cost();
        cost.setCostId(costId);
        cost.setStatus("0");
        cost.setCreatime(creatime);
        cost.setStartime(startime);
        cost.setName(name);
        cost.setBaseDuration(baseDuration);
        cost.setBaseCost(baseCost);
        cost.setUnitCost(unitCost);
        cost.setDescr(descr);
        cost.setCostType(costType);
        costMapper.add(cost);
    }

    @Override
    public void updatecost(String name, Integer baseDuration, Integer baseCost, String descr, Integer costid) {
        costMapper.update(name, baseDuration, baseCost, descr, costid);
    }

    @Override
    public void deletecost(Integer costid) {
        costMapper.delete(costid);
    }

    @Override
    public Cost selectcostbyid(Integer costid) {
        return costMapper.selectCostById(costid);
    }

    @Override
    public List<Cost> selectCostIdByName(String name) {
        return costMapper.selectCostIdByName(name);
    }

    @Override
    public List<Cost> baseasc() {
        return costMapper.costbaseasc();
    }

    public PageInfo<Cost> costbaseasc(Integer pageNo, Integer pageSize) {
        pageNo = pageNo == null ? 1 : pageNo;
        pageSize = pageSize == null ? 2 : pageSize;
        PageHelper.startPage(pageNo, pageSize);
        List<Cost> costList = baseasc();
        PageInfo<Cost> CostPageInfo = new PageInfo<Cost>(costList);
        return CostPageInfo;
    }

    @Override
    public List<Cost> basedesc() {
        return costMapper.costbasedesc();
    }

    public PageInfo<Cost> costbasedesc(Integer pageNo, Integer pageSize) {
        pageNo = pageNo == null ? 1 : pageNo;
        pageSize = pageSize == null ? 2 : pageSize;
        PageHelper.startPage(pageNo, pageSize);
        List<Cost> costList = basedesc();
        PageInfo<Cost> CostPageInfo = new PageInfo<Cost>(costList);
        return CostPageInfo;
    }

    @Override
    public List<Cost> tiemasc() {
        return costMapper.costtiemasc();
    }

    public PageInfo<Cost> costtiemasc(Integer pageNo, Integer pageSize) {
        pageNo = pageNo == null ? 1 : pageNo;
        pageSize = pageSize == null ? 2 : pageSize;
        PageHelper.startPage(pageNo, pageSize);
        List<Cost> costList = tiemasc();
        PageInfo<Cost> CostPageInfo = new PageInfo<Cost>(costList);
        return CostPageInfo;
    }

    @Override
    public List<Cost> tiemdesc() {
        return costMapper.costtiemdesc();
    }

    public PageInfo<Cost> costtiemdesc(Integer pageNo, Integer pageSize) {
        pageNo = pageNo == null ? 1 : pageNo;
        pageSize = pageSize == null ? 2 : pageSize;
        PageHelper.startPage(pageNo, pageSize);
        List<Cost> costList = tiemdesc();
        PageInfo<Cost> CostPageInfo = new PageInfo<Cost>(costList);
        return CostPageInfo;
    }

    @Override
    public void updateStatusByCostId(@Param("status") String status, @Param("costId") Integer costId) {
        costMapper.updateStatusCostById(status, costId);
    }

}
